<template>
    <basic-container>
        <avue-crud ref="crud" :option="option" :page.sync="page" :table-loading="listLoading"
                   :data="list" @on-load="getList"
                   @select="selectOne"
                   @select-all="selectAll"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @refresh-change="refreshChange">
            <template slot="menuLeft">
                <el-button type="primary" size="small" @click="summarySettlement">结算单合并</el-button>
            </template>
            <template slot="menu" slot-scope="scope">
                <el-button type="text" size="mini" icon="el-icon-view"
                           @click="handleDetail(scope.row)">详情
                </el-button>
                <el-button type="text" size="mini" icon="el-icon-edit"
                           v-show="scope.row.settlementStatus !== '1' && scope.row.settlementStyle !=='3'"
                           @click="handleFinish(scope.row)">去结算
                </el-button>
                <el-button type="text" size="mini" icon="el-icon-delete"
                           @click="handleRevoke(scope.row)">撤销
                </el-button>
            </template>
            <template slot-scope="scope" slot="settlementTimeSearch">
                <el-date-picker
                        v-model="settlementDate"
                        type="daterange"
                        range-separator="至"
                        value-format="yyyy-MM-dd"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                </el-date-picker>
            </template>
        </avue-crud>
    </basic-container>

</template>

<script>
    import {fetchList, revokeSettlement, summarySettlement} from '../../api/bill/settlement'
    import {tableOption} from '../../const/crud/bill/settlement'

    export default {
        name: 'settlement',
        data() {
            return {
                option: tableOption,
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20, // 每页显示多少条,
                    isAsc: false// 是否倒序
                },
                list: [],
                listLoading: true,
                selectRowList: [],
                settlementDate: []
            }
        },
        components: {},
        created() {
            this.getHeight();
        },
        mounted() {
            this.option.selectable = this.selectRow;
        },
        methods: {
            getHeight() {
                let tableHeight = Math.max(window.innerHeight - 430, 300);
                this.option.maxHeight = tableHeight + 'px';
            },
            getList(page, params={}) {
                this.listLoading = true;
                if (this.settlementDate.length > 1) {
                    params.startDate = this.settlementDate[0];
                    params.endDate = this.settlementDate[1];
                }
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.list = response.data.data.records
                    this.page.total = response.data.data.total
                    this.listLoading = false
                })
            },
            handleDetail(row) {
                let query = {
                    res: {
                        settlementSn: row.settlementSn
                    }
                };
                this.$router.push({path: '/settlement/settlementDetail', query: query})
            },
            handleFinish(row) {
                let query = {
                    res: {
                        settlementSn: row.settlementSn,
                        settlementAmount: row.settlementAmount
                    }
                };
                this.$router.push({path: '/settlement/settlementFinish', query: query})
            },
            /**
             * 撤销结算
             */
            handleRevoke(row) {
                let params = {
                    settlementSn: row.settlementSn
                };
                this.$confirm('是否确认撤销结算单, 编号为' + row.settlementSn + '; 对应的订单需要重新审核', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return revokeSettlement(params)
                }).then(res => {
                    if (res.data.code === 0) {
                        this.$message.success('撤销成功');
                    }
                    this.getList(this.page)
                })
            },
            /**
             * 可选择行处理
             */
            selectRow(row, index) {
                // 未结算, 结算单类型为普通
                return row.settlementStatus === '0' && row.settlementStyle === '1';
            },
            /**
             * 选择某一行处理
             */
            selectOne(selection, row) {
                // 已选择结算公司需相同
                let selectCompanyId = selection[0] ? selection[0].companyId : '';
                if (selectCompanyId && row.companyId !== selectCompanyId) {
                    this.$refs.crud.toggleRowSelection(row, false);
                    selection = selection.filter(item => item.companyId === selectCompanyId);
                    this.$message.warning('当前选择对应的账单结算公司和此前不同');
                }
                this.selectRowList = selection;
            },
            selectAll(selection) {
                let selectArray = [...selection];
                let selectCompanyId = selectArray[0] ? selectArray[0].companyId : '';
                let size = selectArray.length;
                if (selectCompanyId && size > 1) {
                    selectArray = selectArray.filter(item => {
                        if (item.companyId === selectCompanyId) {
                            return true;
                        }
                        this.$refs.crud.toggleRowSelection(item, false);
                        return false;
                    });
                }
                if (size !== selectArray.length) {
                    this.$message.warning('当前选择对应的账单结算公司有不同');
                }
                this.selectRowList = selectArray;
            },
            summarySettlement() {
                let selectArray = this.selectRowList;
                if (selectArray.length < 2 || selectArray.length > 10) {
                    this.$message.warning("需选择多个结算单合并结算(最多10个)");
                    return;
                }
                let params = {
                    billSnList: selectArray.map(item => item.billSn)
                }
                summarySettlement(params).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success("合并创建结算单成功");
                        this.getList(this.page);
                    }
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
                this.getList(this.page, this.filterForm(info.searchForm));
            },
            searchReset() {
                this.settlementDate = [];
            }
        }
    }
</script>

